//
//  DouBanApiViewController.swift
//  SwiftRx研究
//
//  Created by 世霖 on 2020/7/18.
//  Copyright © 2020 世霖. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
import RxDataSources


class DouBanApiViewController: UIViewController {
 let dispostBag = DisposeBag()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        DouBanProvider.rx.request(.channels)
            .subscribe { event  in
                switch event {
                case let .success(response ) :
                    let str = String(data: response.data, encoding: .utf8)
                    
                    
                    
                case  let .error(error ) :
                    let str = "str"
                    
                }
        }.disposed(by: dispostBag)

        
        
        
//        获取资源
        DouBanProvider.rx.request(.channels)
            .subscribe(onSuccess: { (response ) in
                let json = try? response.mapJSON() as! [String:Any]
                
                
            }) { (error ) in
                
        }.disposed(by: dispostBag)

//        3:获取数据
        DouBanProvider.rx.request(.channels)
        .mapJSON()
            .subscribe(onSuccess: { (data ) in
                let json =  data as! [String:Any]
                   
            }) { (error ) in
                
        }.disposed(by: dispostBag)
        
        
        
//        4: 或者这样获取数据
        let data = DouBanProvider.rx.request(.channels)
        .mapJSON()
            .map { (data ) -> [String : Any] in
                if let json = data as? [String:Any],
                let channers = json["channels"] as?  [[String:Any]] {
                    return channers as! [String : Any]
                }else {
                    return [:]
                }
                
                
        }.asObservable()
        
        
    }

}
